package com.jmu.edu.IMApplication.adapter;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.event.SwitchFragmentEvent;
import com.jmu.edu.IMApplication.fragment.ChatFragment;
import com.jmu.edu.IMApplication.utils.MsgUtils;
import com.jmu.edu.IMApplication.utils.ViewUtils;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

public class ContactItemAdapter extends RecyclerView.Adapter<ContactItemAdapter.MyViewHolder> {


    private List<UserInfo> userInfoList;

    public void setUserInfoList(List<UserInfo> userInfoList) {
        this.userInfoList = userInfoList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        UserInfo userInfo = userInfoList.get(position);
        holder.nicknameText.setText(WillowUtils.getNickName(userInfo));
        holder.signText.setText(WillowUtils.getSignature(userInfo));
        // 如果没有保存好的avatar，那就去加载
        userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (bitmap != null) {
                    Glide.with(holder.avatarImage).load(bitmap).into(holder.avatarImage);
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "chat";
                ChatFragment chatFragment = (ChatFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (chatFragment == null) {
                    chatFragment = new ChatFragment();
                }
                //传递必须的参数
                Bundle bundle = new Bundle();
                bundle.putString("username", userInfo.getUserName());
                bundle.putString("friendName", WillowUtils.getNickName(userInfo));
                bundle.putString("state", "在线");
                chatFragment.setArguments(bundle);
                //这个传头像暂时是用不到了
                //chatFragment.setFriendAvatar(holder.avatar);
                //跳转到聊天界面
                ViewUtils.showFragment(chatFragment, tag, R.anim.from_right, R.anim.out_right);
            }
        });

    }

    @Override
    public int getItemCount() {
        Log.d("好友列表适配器", "当前好友列表数量:" + userInfoList.size());
        return userInfoList.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ShapeableImageView avatarImage;
        private TextView nicknameText, signText;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.item_contact_avatar);
            nicknameText = itemView.findViewById(R.id.item_contact_nickname);
            signText = itemView.findViewById(R.id.item_contact_sign);
        }
    }
}
